package com.qymerge.manage.core.utils;

import org.apache.commons.lang.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Desc 操作日期的工具类
 * User qiuchen
 * Date 2015/4/30
 * Time 17:17
 */
public class DateUtil {

    private static final String DEFAULT_DATE_FORMAT = "yyyy.MM.dd";
    private static final String DATE = "yyyy-MM-dd";
    public static final String LONG_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String UTC_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    public static final String ID_FORMAT = "yyyyMMddHHmmssSSS";

    public DateUtil() {
    }

    public static String getUTCTime(long millis) {
        try {
            //1、取得本地时间：
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(millis);
            //2、取得时间偏移量：
            int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
            //3、取得夏令时差：
            int dstOffset = cal.get(Calendar.DST_OFFSET);
            //4、从本地时间里扣除这些差量，即可以取得UTC时间：
            cal.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
            return new SimpleDateFormat(UTC_FORMAT).format(cal.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将日期格式化为字符串形式yyyy-MM-dd
     *
     * @param time
     * @return 如果date == null，返回""
     */
    public static String formatDate(long time) {
        try {
            final Date date = new Date(time);
            return formatDate(date, DATE);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String formatTime(long time) {
        try {
            final Date date = new Date(time);
            return formatDate(date, "yyyy-MM-dd HH:mm:ss");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 将日期格式化为字符串形式yyyy.MM.dd
     *
     * @param date
     * @return 如果date == null，返回""
     */
    public static String formatDate(final Date date) {
        return formatDate(date, DEFAULT_DATE_FORMAT);
    }

    /**
     * 以指定的日期格式来格式化日期
     *
     * @param date
     * @param format 不允许为null
     * @return 如果date == null，返回""
     */
    public static String formatDate(final Date date, final String format) {
        if (date == null) {
            return "";
        }
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 获取n天后的日期
     *
     * @param date
     * @param days
     */
    public static Date getDateAfter(final Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, days);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取n天前的日期
     *
     * @param date
     * @param days
     */
    public static Date getDateBefore(final Date date, int days) {
        Date yesterday = DateUtil.dateAddOrSubtract(date, Calendar.DAY_OF_MONTH, -days);
        return yesterday;
    }

    public static boolean isTime(long time) {
        return (time > 0);
    }

    /**
     * @param beginDate 开始时间
     * @param endDate   结束时间
     * @return 1  beginDate>endDate
     * -1 beginDate<endDate
     * 0  beginDate=endDate
     * @throws Exception
     */

    public static int compareDate(String beginDate, String endDate) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date dt1 = df.parse(beginDate);
            Date dt2 = df.parse(endDate);
            if (dt1.getTime() > dt2.getTime()) {
                //begingDate在endDate前
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                //begingDate在endDate后
                return -1;
            } else {
                return 0;
            }
        } catch (ParseException e) {
            throw new Exception("日期格式化异常");
        }

    }

    /**
     * @param begingDate
     * @param endDate
     * @return 1  beginDate>endDate
     * -1 beginDate<endDate
     * 0  beginDate=endDate
     */
    public static int compareDate(Date begingDate, Date endDate) {
        if (begingDate.getTime() > endDate.getTime()) {
            //begingDate在endDate前
            return 1;
        } else if (begingDate.getTime() < endDate.getTime()) {
            //begingDate在endDate后
            return -1;
        } else {
            return 0;
        }
    }


    /**
     * 获得指定日期的第一秒(yyyy-MM-dd 00:00:00:000)
     *
     * @param fullDate 日期
     * @return 日期
     */
    public static Date getFirstSecondOfTheDay(Date fullDate) {
        if (null == fullDate) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(fullDate);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获得指定日期后一天的第一秒(yyyy-MM-dd 00:00:00:000)
     *
     * @param fullDate 日期
     * @return 日期
     */
    public static Date getFirstSecondOfNextDay(Date fullDate) {
        if (null == fullDate) {
            return (Date) null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(fullDate);
        cal.add(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        return cal.getTime();
    }

    /**
     * 获得指定日期的最后一秒 (yyyy-MM-dd 23:59:59:000)
     *
     * @param fullDate 日期
     * @return 日期
     */
    public static Date getLastSecondOfTheDay(Date fullDate) {
        if (null == fullDate) {
            return (Date) null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(fullDate);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获得指定日期前一天的最后一秒 (yyyy-MM-dd 23:59:59:000)
     *
     * @param fullDate 日期
     * @return
     */
    public static Date getLastSecondOfLastDay(Date fullDate) {
        if (null == fullDate) {
            return (Date) null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(fullDate);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获得指定日期N个月后最后一天的前一天的最后一秒 (yyyy-MM-dd 23:59:59:000)
     *
     * @param fullDate 日期
     * @param months   月份数
     * @return
     */
    public static Date getLastSecondOfLastDayAfterNMonths(Date fullDate, int months) {
        if (null == fullDate) {
            return (Date) null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(fullDate);
        cal.add(Calendar.MONTH, months);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获得指定日期N-1天的最后一秒 (yyyy-MM-dd 23:59:59:000)
     *
     * @param fullDate 日期
     * @param days     天数
     * @return
     */
    public static Date getLastSecondOfLastDayAfterNDays(Date fullDate, int days) {
        if (null == fullDate) {
            return (Date) null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(fullDate);
        cal.add(Calendar.DAY_OF_MONTH, days - 1);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获得指定日期N-1天的第一秒 (yyyy-MM-dd 00:00:00:000)
     *
     * @param fullDate 日期
     * @param days     天数
     * @return
     */
    public static Date getFirstSecondOfLastDayAfterNDays(Date fullDate, int days) {
        if (null == fullDate) {
            return (Date) null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(fullDate);
        cal.add(Calendar.DAY_OF_MONTH, days - 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 00);
        cal.set(Calendar.SECOND, 00);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获得指定日期N+1天的最后一秒 (yyyy-MM-dd 23:59:59:000)
     *
     * @param fullDate 日期
     * @param days     天数
     * @return
     */
    public static Date getLastSecondOfLastDayBeforeNDays(Date fullDate, int days) {
        if (null == fullDate) {
            return (Date) null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(fullDate);
        cal.add(Calendar.DAY_OF_MONTH, days + 1);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获得指定日期N+1天的第一秒 (yyyy-MM-dd 00:00:00:000)
     *
     * @param fullDate 日期
     * @param days     天数
     * @return
     */
    public static Date getFirstSecondOfLastDayBeforeNDays(Date fullDate, int days) {
        if (null == fullDate) {
            return (Date) null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(fullDate);
        cal.add(Calendar.DAY_OF_MONTH, days + 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 00);
        cal.set(Calendar.SECOND, 00);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 增加年数
     *
     * @param fullDate 日期
     * @param years    年数
     * @return 增加月份数的日期
     */
    public static Date addYear(Date fullDate, int years) {
        if (null == fullDate) {
            return (Date) null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(fullDate);
        calendar.add(Calendar.YEAR, years);
        return calendar.getTime();
    }

    /**
     * 增加月份数
     *
     * @param fullDate 日期
     * @param months   月份数
     * @return 增加月份数的日期
     */
    public static Date addMonth(Date fullDate, int months) {
        if (null == fullDate) {
            return (Date) null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(fullDate);
        calendar.add(Calendar.MONTH, months);
        return calendar.getTime();
    }

    /**
     * 增加天数
     *
     * @param fullDate 日期
     * @param days     天数
     * @return 增加天数后的日期
     */
    public static Date addDay(Date fullDate, int days) {
        if (null == fullDate) {
            return (Date) null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(fullDate);
        cal.add(Calendar.DAY_OF_MONTH, days);
        return cal.getTime();
    }


    /**
     * 减少天数
     *
     * @param fullDate 日期
     * @param days     天数
     * @return 增加天数后的日期
     */
    public static Date subDay(Date fullDate, int days) {
        if (null == fullDate) {
            return (Date) null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(fullDate);
        cal.add(Calendar.DAY_OF_MONTH, -days);
        return cal.getTime();
    }

    /**
     * linux下转换日期
     *
     * @param str
     * @return
     */
    public static Date toDate(String str) throws ParseException {
        Date dt = new Date();
        String[] parts = str.split("-");

        if (parts.length == 3) {
            int years = Integer.parseInt(parts[0]);
            int months = Integer.parseInt(parts[1]);
            int days = Integer.parseInt(parts[2]);
            int hours = 0;
            int minutes = 0;
            int seconds = 0;

            GregorianCalendar gc = new GregorianCalendar(years, months, days,
                    hours, minutes, seconds);

            dt = gc.getTime();
        } else {
            throw new ParseException("转换日期出错", 0);
        }
        return dt;
    }

    /**
     * 计算时间差，转换为【xx天xx时xx分】的格式
     *
     * @param endDate
     * @param beginDate
     * @return
     */
    public static long calcShortenedDay(Date beginDate,Date endDate) {
        if (endDate == null || beginDate == null) {
            return 0;
        }
        long shortenedSeconds = (endDate.getTime() - beginDate.getTime()) / 1000;
        if (shortenedSeconds < 0) {
            return 0;
        }
        long day = shortenedSeconds / (24 * 3600);
        return day;
    }

    /**
     * 计算时间差，转换为【xx天xx时xx分】的格式
     *
     * @param endDate
     * @param beginDate
     * @return
     */
    public static String calcShortenedDate(Date endDate, Date beginDate) {
        if (endDate == null || beginDate == null) {
            return "";
        }
        StringBuffer shortenedDate = new StringBuffer();
        long shortenedSeconds = (endDate.getTime() - beginDate.getTime()) / 1000;
        if (shortenedSeconds < 0) {
            return "";
        }
        long day = shortenedSeconds / (24 * 3600);
        long hour = shortenedSeconds % (24 * 3600) / 3600;
        long minute = shortenedSeconds % 3600 / 60;
        shortenedDate.append(day);
        shortenedDate.append("天");
        shortenedDate.append(hour);
        shortenedDate.append("时");
        shortenedDate.append(minute);
        shortenedDate.append("分");
        return shortenedDate.toString();
    }

    /**
     * *******************************************
     * For AC
     * *********************************************
     */
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 格式化日期.
     *
     * @param date 为null时返回""
     *             期望的日期格式，为空时使用默认的日期格式DateUtil.YYYY_MM_DD_HH_MM_SS
     * @return
     */
    public static String format(Date date) {
        return format(date, null);
    }


    /**
     * 格式化日期.
     *
     * @param date    为null时返回""
     * @param pattern 期望的日期格式，为空时使用默认的日期格式DateUtil.YYYY_MM_DD_HH_MM_SS
     * @return
     */
    public static String format(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        if (pattern == null || pattern.trim().length() == 0) {
            pattern = YYYY_MM_DD_HH_MM_SS;
        }
        SimpleDateFormat formater = new SimpleDateFormat(pattern);
        return formater.format(date);
    }

    /**
     * 将日期字符串解析成指定格式的Date对象
     *
     * @param dateTime 日期字符串
     * @param format   指定格式
     * @return （正确格式）日期对象
     * @throws ParseException
     */
    public static Date parse(String dateTime, String format) throws ParseException {
        if (dateTime == null || dateTime.length() <= 0) {
            return null;
        }
        String sDateTime = ((dateTime.indexOf('.') > 0)) ? dateTime.substring(0, dateTime.indexOf('.')) : dateTime;
        DateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.parse(sDateTime);
    }

    /**
     * 将日期字符串解析成"yyyy-MM-dd HH:mm:ss"格式的Date对象
     *
     * @param dateTime 日期字符串
     *                 指定格式
     * @return （正确格式）日期对象
     * @throws ParseException
     */
    public static Date parseDateTime(String dateTime) throws ParseException {
        // return parse(dateTime, YYYY_MM_DD);
        return parseDate(dateTime);
    }

    /**
     * 将日期字符串解析成"yyyy-MM-dd HH:mm:ss"格式的Date对象
     *
     * @param dateTime 日期字符串
     *                 指定格式
     * @return （正确格式）日期对象
     * @throws ParseException
     */
    public static Date parseDateTimeSafe(String dateTime) {
        // return parse(dateTime, YYYY_MM_DD);
        try {
            return parseDate(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

    /**
     * 将日期字符串解析成"yyyy-MM-dd"格式的Date对象
     *
     * @param dateTime 日期字符串
     *                 指定格式
     * @return （正确格式）日期对象
     * @throws ParseException
     */
    public static Date parseDate(String dateTime) throws ParseException {
        return parse(dateTime, YYYY_MM_DD);
    }

    public static Date getYesterday(Date date) {
        Date yesterday = DateUtil.dateAddOrSubtract(date, Calendar.DAY_OF_MONTH, -1);
        return yesterday;
    }

    public static Date dateAddOrSubtract(Date date, int field, int count) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(field, count);
        return c.getTime();
    }

    public static int getDaysBetween(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return 0;
        }
        Calendar d1 = new GregorianCalendar();
        d1.setTime(date1);
        Calendar d2 = new GregorianCalendar();
        d2.setTime(date2);
        if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
            Calendar swap = d1;
            d1 = d2;
            d2 = swap;
        }
        int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
        int y2 = d2.get(Calendar.YEAR);
        if (d1.get(Calendar.YEAR) != y2) {
            d1 = (Calendar) d1.clone();
            do {
                days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
                d1.add(Calendar.YEAR, 1);
            } while (d1.get(Calendar.YEAR) != y2);
        }
        return days;
    }

    /**
     * 取得两个日期之间相差的月份
     * 如果日期相差1个月以内，则显示一个月
     *
     * @param startdate
     * @param enddate
     * @return
     */
    public static int getMonth(Date startdate, Date enddate) {
        if (startdate == null || enddate == null) {
            return 0;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startdate);
        int startyear = calendar.get(Calendar.YEAR);
        int startmonth = calendar.get(Calendar.MONTH);
        calendar.setTime(enddate);
        int endmonth = calendar.get(Calendar.MONTH);
        int endyear = calendar.get(Calendar.YEAR);

        int addMonth = (endyear - startyear) * 12;
        int result = (endmonth - startmonth) + addMonth;
        if (result <= 0) {
            result = 1;
        }
        return result;
    }

    /**
     * 增加月份，录入startDate为2015-02-11，如果month为3，即endTime为2015-06-01
     *
     * @param startDate
     * @param month
     * @return
     */
    public static Date addMonthsToFirstDay(Date startDate, int month) {
        Date endDate = addMonth(startDate, month + 1);
        String endDateStr = format(endDate, "yyyy-MM");
        endDateStr += "-01";
        try {
            endDate = parseDate(endDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return endDate;
    }

    /**
     * 获取星期
     *
     * @param time
     * @return
     */
    public static int getWeekDay(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time));
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w == 0) {
            w = 7;
        }
        return w;
    }

    public static Date getDateByYearAndMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONDAY, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Calendar getCalendar(String date) {
        Calendar calendar = Calendar.getInstance();
        if (StringUtils.isNotEmpty(date)) {
            calendar.setTime(parseDateTimeSafe(date));
        } else {
            calendar.setTime(new Date());
        }
        return calendar;
    }

    /**
     * 根据起始日期获取起始日期范围内的每一天日期
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static List<String> getRangeDate(String beginDate, String endDate) {
        List<String> list = new ArrayList<String>();
        Calendar begin = getCalendar(beginDate);
        Calendar end = getCalendar(endDate);
        if (StringUtils.isNotEmpty(beginDate) && StringUtils.isEmpty(endDate)) {
            begin.add(Calendar.DAY_OF_YEAR, 1);
        }
        if (StringUtils.isNotEmpty(endDate)) {
            end.add(Calendar.DAY_OF_YEAR, 1);
        }
        while (begin.before(end)) {
            list.add(formatDate(begin.getTime(), DATE));
            begin.add(Calendar.DAY_OF_YEAR, 1);
        }
        return list;
    }

    public static long getTimeByStr(String date, int location) {
        Calendar cal = getCalendar(date);
        if (0 == location) {
            cal.set(Calendar.HOUR_OF_DAY, 0);
        } else if (1 == location) {
            cal.set(Calendar.HOUR_OF_DAY, 24);
        }
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }


    /**
     * 获取 今天、本周、本月的开始和结束时间
     *
     * @param type     ['today', 'week', 'month']
     * @param location 0开始 1截止
     * @return
     */
    public static long getTimeByConf(String type, int location) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        if ("today".equals(type)) {
            if (0 == location) {
                cal.set(Calendar.HOUR_OF_DAY, 0);
            } else if (1 == location) {
                cal.set(Calendar.HOUR_OF_DAY, 24);
            }
        } else if ("yesterday".equals(type)) {
            if (0 == location) {
                cal.add(Calendar.DAY_OF_MONTH, -1);
            } else if (1 == location) {
                cal.add(Calendar.DAY_OF_MONTH, 0);
            }
        } else if ("week".equals(type)) {
            if (0 == location) {
                cal.setFirstDayOfWeek(Calendar.MONDAY);
                cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            } else if (1 == location) {
                cal.setFirstDayOfWeek(Calendar.MONDAY);
                cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
                cal.add(Calendar.DAY_OF_WEEK, 7);
            }
        } else if ("month".equals(type)) {
            if (0 == location) {
                cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
            } else if (1 == location) {
                cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
                cal.set(Calendar.HOUR_OF_DAY, 24);
            }
        }
        return cal.getTimeInMillis();
    }

    public static long getLongTimeByConf(String date, int location) {
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(parseDate(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        if (0 == location) {
            cal.set(Calendar.HOUR_OF_DAY, 0);
        } else if (1 == location) {
            cal.set(Calendar.HOUR_OF_DAY, 24);
        }
        return cal.getTimeInMillis();
    }

    public static long getLongTimeByYYYYMMDDHHmm(String date) {
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(parse(date, YYYY_MM_DD_HH_MM));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

}
